<?php
namespace App\Service;

use Hyperf\DbConnection\Db;
use App\Service\PlayletService;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;

class BlocksService extends CommicService
{

    public function __construct(PlayletService $playletService)
    {
        $this->playletService = $playletService;
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }
    public function get_blocks_list(int $applet_id){
        # 获取榜单列表
        $rec = Db::table('playlet_blocks')->where([
            ['applet_id', '=', $applet_id],
            ['status', '=', '1'],
            ['deleted', '=', '0'],
        ])->orderBy('sort',"desc")->select('id','name','template_type','cateid',"ranking_ids")->get();
        $is_empty = $this->object_to_array($rec);
        if (empty($is_empty)){
            $rec = Db::table('playlet_blocks')->where([
                ['applet_id', '=', 0],
                ['status', '=', '1'],
                ['deleted', '=', '0'],
            ])->orderBy('sort',"desc")->select('id','name','template_type','cateid',"ranking_ids")->get();
        }
//        foreach ($rec as $key =>$value){
//            $value->ranking_list = self::get_anking_list($value);
//        }
        return $rec;
    }

    public function get_blocks_info($blocks_id){
        $redis_key = "blocks:blocks_info:";
        $blocks_info = $this->redisService->hGet($redis_key,$blocks_id);
        $blocks_info = json_decode($blocks_info,true);
        if (empty($blocks_info)){
            $blocks_info = Db::table("playlet_blocks")->where([
                "id"=>$blocks_id,
            ])->first();
            $blocks_info = $this->object_to_find_array($blocks_info);
            $this->redisService->hSet($redis_key,$blocks_id,json_encode($blocks_info));
        }
        return $blocks_info;
    }

    public function get_ranking_list($blocks_id,$playlet_id=0){
        # 榜单列表
        $blocks_info = $this->get_blocks_info((string)$blocks_id);
        print_r($blocks_info);

        $rec = [];
        if (!empty($blocks_info)){
            $ranking_ids = explode(',',$blocks_info["ranking_ids"]);
            $rec = Db::table('playlet_blocks_ranking')->whereIn('id',$ranking_ids)->where([
                ['status', '=', '1'],
                ['deleted', '=', '0'],
            ])->select('id','name','template_type',"playlet_ids")->get();
            $is_empty = $this->object_to_array($rec);
            if (empty($is_empty)){
                $rec = Db::table('playlet_blocks_ranking')->whereIn('id',$ranking_ids)->where([
                    ['status', '=', '1'],
                    ['deleted', '=', '0'],
                ])->orderBy('sort',"desc")->select('id','name','template_type',"playlet_ids")->get();
            }

            if ($rec){
                foreach ($rec as $key=>$value){
                    if (in_array($value->template_type,[101,102])){
                        $value->lists = $this->get_ranking_banner_list($value->playlet_ids);
                    }elseif (in_array($value->template_type,[103,104,105])){
                        if($playlet_id != 0 && !in_array($playlet_id, ['88', '89'])) {
                            $value->playlet_ids =  $playlet_id.','.$value->playlet_ids;
                        }
                        $value->lists = $this->get_ranking_playlet_list($value->template_type,$value->playlet_ids);
                    }elseif (in_array($value->template_type,[2,3,4])){
                        $value->lists = $this->get_news_list($value->template_type,$value->playlet_ids);
                    }
                }
            }
        }
        return $rec;
    }

    public function get_ranking_banner_list($ids){
        #ranking_banne列表
        $ids = explode(",",$ids);
        $rec_tmlp = [];
        $rec_tmp = [];
        $rec = Db::table("base_banner")->whereIn(
            "id",$ids
        )->select("id","name","cover","jump_id","jump_type","template_type")->get();
        $rec = $this->object_to_array($rec);
        if ($rec){
            foreach ($rec as $key=>$value){
                $rec_tmp[$value["id"]] = $value;
            }
            $i = 0;
            foreach ($ids as $key=>$value){
                if (!empty($rec_tmp[$value])){
                    $rec_tmlp[$i] = $rec_tmp[$value];
                    $i ++;
                }
            }
        }
        return $rec_tmlp;
    }

    public function get_ranking_playlet_list($template_type,$ids){
        # 排行短剧列表
        $ids = explode(",",$ids);
        $rec_tmlp = [];
        $rec_tmp = [];
        $map = [];
        $rec = Db::table("playlet")->whereIn(
            "id",$ids
        )->select("id as playlet_id","name","cover","tag","last_episode_number","thumbnail")->get();
        $rec = $this->object_to_array($rec);
        if ($rec){
            foreach ($rec as $key=>$value){
                $value["cover"] = $this->get_curl_tool($value["cover"]);
                $value["play_url"] = "";
                $value["template_type"] = $template_type;
                if (in_array($template_type,[104])){
                    $episodes_info = $this->playletService->episodes_info($value["playlet_id"],"1");
                    $value["play_url"] = $episodes_info["play_url"];
                }
                $rec_tmp[$value["playlet_id"]] = $value;
            }
            $i = 0;
            foreach ($ids as $key=>$value){

                if (!empty($rec_tmp[$value])){
                    if(isset($map[$value])) {
                        continue;
                    }
                    $rec_tmlp[$i] = $rec_tmp[$value];
                    $map[$value] = 1;
                    $i ++;
                }
            }
        }
        return $rec_tmlp;

    }

    public function get_news_list($template_type,$ids){
        # 查询新闻列表
        $ids = explode(",",$ids);
        $rec_tmlp = [];
        $rec_tmp = [];
        $rec = Db::table("data_news_item")->whereIn(
            "id",$ids
        )->select("id as news_id","name","cover","remark","author","num_read","img_w","img_h")->get();
        $rec = $this->object_to_array($rec);
        if ($rec){
            foreach ($rec as $key=>$value){
                $value["cover"] = $this->get_curl_tool($value["cover"]);
                $value["template_type"] = $template_type;
                $value["num_read"] = $value["num_read"]."万+";
                $rec_tmp[$value["news_id"]] = $value;
            }
            $i = 0;
            foreach ($ids as $key=>$value){
                if (!empty($rec_tmp[$value])){
                    $rec_tmlp[$i] = $rec_tmp[$value];
                    $i ++;
                }
            }
        }
        return $rec_tmlp;
    }
    public function user_icons_list($applet_id){
        $icons_data_list = [];
        # 我的icons
        $banner_list = Db::table("data_user_icons")->where([
            "applet_id"=>$applet_id,
            "status"=>1,
            "deleted"=>0,
            "s_type"=>1,
        ])->select("name","icon","jump_type")->orderBy("sort","desc")->get();

        $banner_list = $this->object_to_array($banner_list);
        if (empty($banner_list)){
            $banner_list = Db::table("data_user_icons")->where([
                "applet_id"=>0,
                "status"=>1,
                "deleted"=>0,
                "s_type"=>1,
            ])->select("name","icon","jump_type")->orderBy("sort","desc")->get();
        }
        $icons_list = Db::table("data_user_icons")->where([
            "applet_id"=>$applet_id,
            "status"=>1,
            "deleted"=>0,
            "s_type"=>2,
        ])->select("name","icon","jump_type")->orderBy("sort","desc")->get();
        $icons_list = $this->object_to_array($icons_list);
        if (empty($icons_list)){
            $icons_list = Db::table("data_user_icons")->where([
                "applet_id"=>0,
                "status"=>1,
                "deleted"=>0,
                "s_type"=>2,
            ])->select("name","icon","jump_type")->orderBy("sort","desc")->get();
        }
        $icons_data_list["banner_list"] = $banner_list;
        $icons_data_list["icons_list"] = $icons_list;
        unset($banner_list);
        unset($icons_list);
        return $icons_data_list;
    }

    public function news_info($news_id){
        # 文章详情
        $recs = Db::table("data_news_item")->where([
            "id"=>$news_id,
            "status"=>1,
            "deleted"=>0,
        ])->select("id as news_id","name","cover","remark","num_read","template_type","author","img_h","img_w","content as info")->first();
        $is_empty = $this->object_to_find_array($recs);
        if ($is_empty){
            $recs->num_read = $recs->num_read."万+";
            $recs->cover = $this->get_curl_tool($recs->cover);
        }
        return $recs;
    }

    public function wallpaper_list($applet_id,$template_type){
        $rec_data = [];
        # 我的icons
        $rec_data = Db::table("data_news_item")->where([
            "applet_id"=>$applet_id,
            "status"=>1,
            "deleted"=>0,
            "template_type"=>$template_type,
        ])->select("id as news_id","name","cover","remark","author","num_read","img_w","img_h")->orderBy("sort","desc")->get();

        $rec_data = $this->object_to_array($rec_data);
        if (empty($rec_data)){
            $rec_data = Db::table("data_news_item")->where([
                "applet_id"=>0,
                "status"=>1,
                "deleted"=>0,
                "template_type"=>$template_type,
            ])->select("id as news_id","name","cover","remark","author","num_read","img_w","img_h")->orderBy("sort","desc")->get();
        }
        foreach ($rec_data as $key =>$val){
            if (!empty($val->cover)){
//                $val->cover = $this->get_curl_tool($val->cover);
                $val->cover = "https://ky-dj.oss-accelerate.aliyuncs.com/videos/27/275e3273e08f4656d3d3271d6185cc41/1697871190445_f326f326.png";
            }
        }
        return $rec_data;
    }


    public function get_recommend($applet_id,$stype =1){
        $recommend_info = Db::table("data_playlet_recommend")->where(["applet_id"=>$applet_id,"status"=>1,"stype"=>$stype])->select("playlet_ids")->first();
        $recommend_info_rec = $this->object_to_find_array($recommend_info);
        if (empty($recommend_info_rec)){
            $recommend_info = Db::table("data_playlet_recommend")->where(["applet_id"=>0,"status"=>1,"stype"=>$stype])->select("playlet_ids")->first();
        }
        $playlet_ids = explode(',',$recommend_info->playlet_ids);
        if (empty($playlet_ids)){
            return [];
        }else{
            # 获取短剧详情
            $playlet_list = Db::table("playlet")->whereIn("id",$playlet_ids)->select("id as playlet_id","name","cover","tag","introduction","last_episode_number","hot_number")->get();
            $playlet_list = $this->object_to_array($playlet_list);
            $recd_data_tmp = [];
            $recd_data = [];
            if ($playlet_list){
                foreach ($playlet_list as $key => $value){
                    $value["id"] = $key+1;
                    $value["playlet_number"] = "1";
                    $value["cover"] = $this->get_curl_tool($value["cover"]);
                    $recd_data_tmp[$value["playlet_id"]] = $value;
                }
                foreach ($playlet_ids as $key=>$val){
                    $recd_data[$key] = $recd_data_tmp[$val];
                }
            }
            return $recd_data;
        }
    }

    public function share_config($applet_id){
        $applet_info = $this->get_applet_info($applet_id);
        $applet_info_tmp = [
            "share_url"=>$applet_info["share_url"],
            "share_title"=>$applet_info["share_title"],
            "share_content"=>$applet_info["share_content"],
        ];
        unset($applet_info);
        return $applet_info_tmp;
    }
}